﻿using SL651Center2.x.Enums;
using SL651Center2.x.Messages;
using SL651Center2.x.ReadMessage;
using SL651Center2.x.Send;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using TouchSocket.Core;
using TouchSocket.Sockets;

namespace SL651Center2.x
{
    public class ReceivedMessagePlugin : PluginBase, ITcpReceivedPlugin
    {

        public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
        {
            ITcpSessionClient sessionClient = (TcpSessionClient)client;
            //这里处理数据接收
            //根据适配器类型，e.ByteBlock与e.RequestInfo会呈现不同的值，具体看文档=》适配器部分。
            ByteBlock byteBlock = e.ByteBlock;
            
            Console.Write("\n接收到的字节数据:");
            for (int i = 0; i < byteBlock.Length; i++)
            {
                Console.Write($"{byteBlock.Span[i]} ");
            }

            Console.WriteLine();
            var hex1 = BitConverter.ToString(byteBlock.ToArray(), 0, byteBlock.Length);
            Console.WriteLine($"\n16进制1：{hex1}");
            var hex2 = Helper.ToHEXString(byteBlock.ToArray(), byteBlock.Length);
            Console.WriteLine($"\n16进制2：{hex2}");

            Console.WriteLine();

            //判断包头是HEX还是ASCII
            if (byteBlock.Span.StartsWith(MessageFrame.ASCII))
            {
                Console.WriteLine($"报文长度：{byteBlock.Length}，正文长度{byteBlock.Length - 24 - 5}");

                //从服务器收到信息。
                var mes = Encoding.ASCII.GetString(byteBlock.Span);
                client.Logger.Info($"客户端接收到信息：{mes}\n");


                MessageFrame header = new MessageFrame();
                header.SetFrameCode(byteBlock.ReadToSpan(1).ToArray());
                Console.WriteLine(header.FrameCoding.ByBytesToHexString());

                //计算校验码
                var crc = Helper.Crc16(byteBlock.Span, 0, byteBlock.Length - 4);
                Console.WriteLine($"CRC16校验码为:{crc.ByBytesToHexString()}");
                var crc1 = Encoding.ASCII.GetString(byteBlock.ToArray(), e.ByteBlock.Length - 4, 4);
                Console.WriteLine($"报文CRC16校验码为:{crc1}");

                if (header.CheckCRC(byteBlock))
                {
                    Console.WriteLine("校验码正确");
                }
                else
                {
                    Console.WriteLine("校验码错误");
                }

                #region 报头报尾读取
                /*var ccode = byteBlock.ReadToArray(2);
                var hccod = ccode.ToASCIIString();
                Console.WriteLine($"中心站地址:{hccod}-{uint.Parse(hccod, NumberStyles.HexNumber)}");

                var f = byteBlock.ReadToArray(10);
                var siteCode = Encoding.ASCII.GetString(f);
                Console.WriteLine($"遥测站地址:{siteCode}--{f.ToASCIIString()}");
                client.ResetId(siteCode);

                var pwdh = byteBlock.ReadToArray(4);
                var pwd1 = Encoding.ASCII.GetString(pwdh);
                Console.WriteLine($"密码:{pwd1}--{Convert.ToUInt16(pwd1, 16)}--{pwdh.ToASCIIString()}");

                var fccode = byteBlock.ReadToArray(2);
                var fnhs = fccode.ToASCIIString();
                var fnh = Helper.HEXStrToByte(fnhs);
                //var fnh = byte.Parse(fnhs, NumberStyles.HexNumber);
                var fnName = FunctionCodeInfo.GetName((FunctionCode)fnh[0]);
                Console.WriteLine($"{fnName},功能码:{fnhs}");

                var u = byteBlock.ReadToArray(4);
                var uh = Encoding.ASCII.GetString(u);
                var c1 = uh[..1];
                Console.WriteLine($"上下行标识:{c1}上行");
                var len = Convert.ToInt32(uh[1..], 16);
                var len1 = Convert.ToInt32(uh, 16);
                Console.WriteLine($"报文长度:{len}");

                Console.WriteLine($"报文起始符:{Helper.ToHEXString(byteBlock.ReadToArray(1))}");
                //Console.WriteLine($"报文起始符:{byteBlock.ReadByte()}");

                var o = byteBlock.ToArray(byteBlock.Len - 5, 1);
                var o1 = byteBlock[byteBlock.Len - 5];
                Console.WriteLine($"报文结束符:{Helper.ToHEXString(o)}");*/
                #endregion

                header.ReadHead(byteBlock);
                await sessionClient.ResetIdAsync(header.TelemetryAddressStr);

                Console.WriteLine($"中心站地址:{header.CentralAddressStr}");

                Console.WriteLine($"遥测站地址:{header.TelemetryAddressStr}");

                Console.WriteLine($"密码:{header.PassWordStr}");

                var fn = FunctionCodeInfo.GetName(header.FunctionCode);
                Console.WriteLine($"{fn},功能码:{Helper.ToHEXString((byte)header.FunctionCode)}");

                Console.WriteLine($"报文长度:{header.MessageLength}");

                Console.WriteLine($"报文起始符:{header.StartMark}-{(byte)header.StartMark}");
                Console.WriteLine($"报文结束符:{header.EndMark}-{(byte)header.EndMark}");

                Console.WriteLine("----正文内容----");

                //正文读取
                //ReadASCIIData.Instance.ReadASCIIMessage(byteBlock, header);
                //var messageData = ReadASCIIData.Instance.Message;
                //return Task.CompletedTask;

                //判断是否是多包传输
                if (header.StartMark == StartMarks.SYN)
                {
                    Console.WriteLine($"包总数及序列号:{header.PackageIndex}/{header.PackageCount}");
                    //读取正文
                    var bytes = byteBlock.ToArray(byteBlock.Position, header.MessageLength);
                    //保存正文内容
                    SYN.Write(bytes);

                    if (header.PackageIndex == header.PackageCount)
                    {
                        var block = SYN.Block;
                        ReadASCIIData.Instance.ReadASCIIMessage(block, header.FunctionCode);
                        SYN.Clera();
                    }

                    e.Handled = true;//表示该数据已经被本插件处理，无需再投递到其他插件。

                    return;
                }

                //读取流水号
                byte[] serialnum = new byte[4];
                byteBlock.Read(serialnum);
                var snum = serialnum.ToASCIIString();
                Convert.ToInt32(snum, 16);
                Console.WriteLine($"流水号:{Convert.ToUInt16(snum, 16)}--{snum}");

                //读取发报时间
                byte[] timeb = new byte[12];
                byteBlock.Read(timeb);
                var timeHEX = timeb.ToASCIIString();
                var timeString = $"{timeHEX[..2]}年{timeHEX.Substring(2, 2)}月{timeHEX.Substring(4, 2)}日{timeHEX.Substring(6, 2)}:{timeHEX.Substring(8, 2)}:{timeHEX.Substring(10)}";
                Console.WriteLine($"发报时间:{timeString}");

                //根据功能码解析报文
                switch (header.FunctionCode)
                {
                    case FunctionCode.FC_30:
                    case FunctionCode.FC_32:
                    case FunctionCode.FC_33:
                    case FunctionCode.FC_34://小时报
                    case FunctionCode.FC_37:
                    case FunctionCode.FC_3A:
                    case FunctionCode.FC_44:
                        //要素数据
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);


                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var typestr = arr[2];
                            if (Enum.IsDefined(typeof(TelemetryStationCode), typestr))
                            {
                                var typecode = (TelemetryStationCode)Enum.Parse(typeof(TelemetryStationCode), typestr);
                                var typename = TelemetryStationInfo.GetInfo(typecode);
                                Console.WriteLine($"遥测站分类码:{Helper.ToHEXString((byte)typecode)}-{typestr}-{typename}");
                            }

                            for (int i = 3; i < arr.Length - 1; i += 2)
                            {
                                if (arr[i] == "TT")
                                {
                                    var time = arr[i + 1];
                                    var timeStr = $"{time[..2]}年{time.Substring(2, 2)}月{time.Substring(4, 2)}日{time.Substring(6, 2)}:{time.Substring(8)}";
                                    Console.WriteLine($"观测时间：{timeStr}");
                                    if (time.Length > 10)
                                    {
                                        var drp = time[10..];
                                        if (drp == "DRP")
                                        {
                                            var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), drp);

                                            var (name, unit) = ElementInfo.Instance.GetInfo(elementCode);
                                            Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name}");
                                            for (int j = 0; j < 12; j++)
                                            {
                                                i += 1;
                                                var d1 = arr[i + 1];
                                                var v = Convert.ToDecimal(Convert.ToUInt16(d1, 16)) / 10;
                                                if (Regex.IsMatch(d1, "^[Ff]*$"))
                                                {
                                                    v = 0;
                                                }
                                                Console.WriteLine($"数据：{v}{unit}");

                                            }
                                        }
                                    }
                                    continue;
                                }

                                if (arr[i] == "DRP")
                                {
                                    var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[i]);

                                    var (name, unit) = ElementInfo.Instance.GetInfo(arr[i]);
                                    Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name}");
                                    for (int j = 0; j < 12; j++)
                                    {
                                        var d1 = arr[i + 1];
                                        var v = Convert.ToDecimal(Convert.ToUInt16(d1, 16)) / 10;
                                        if (Regex.IsMatch(d1, "^[Ff]*$"))
                                        {
                                            v = 0;
                                        }
                                        Console.WriteLine($"数据：{v}{unit}");
                                        if (j < 11)
                                        {
                                            i += 1;
                                        }
                                    }
                                    continue;
                                }

                                if (arr[i].StartsWith("DRZ"))
                                {
                                    var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[i]);

                                    var (name, unit) = ElementInfo.Instance.GetInfo(arr[i]);
                                    Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name}");
                                    for (int j = 0; j < 12; j++)
                                    {
                                        var d1 = arr[i + 1];
                                        var v = Convert.ToDecimal(Convert.ToUInt16(d1, 16)) / 100;
                                        if (Regex.IsMatch(d1, "^[Ff]*$"))
                                        {
                                            v = 0;
                                        }
                                        Console.WriteLine($"数据：{v}{unit}");
                                        if (j < 11)
                                        {
                                            i += 1;
                                        }
                                    }
                                    continue;
                                }

                                if (Enum.IsDefined(typeof(ElementCode), arr[i]))
                                {
                                    var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[i]);

                                    var (name, unit) = ElementInfo.Instance.GetInfo(arr[i]);
                                    Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name},数据：{arr[i + 1]}{unit}");
                                }
                            }
                            break;
                        }
                    case FunctionCode.FC_31://有时间步长码
                    case FunctionCode.FC_38://有时间步长码
                        //均匀时段报
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var typestr = arr[2];
                            if (Enum.IsDefined(typeof(TelemetryStationCode), typestr))
                            {
                                var typecode = (TelemetryStationCode)Enum.Parse(typeof(TelemetryStationCode), typestr);
                                var typename = TelemetryStationInfo.GetInfo(typecode);
                                Console.WriteLine($"遥测站分类码:{Helper.ToHEXString((byte)typecode)}-{typestr}-{typename}");
                            }

                            var tt = arr[4];
                            var tString = $"{tt[0..2]}年{tt[2..4]}月{tt[4..6]}日{tt[6..8]}:{tt[8..10]}";
                            Console.WriteLine($"观测时间:{tString}");

                            //时间步长码
                            var drCode = arr[5];
                            if (drCode.StartsWith("DRD"))
                            {
                                Console.WriteLine($"时间步长码：{drCode[^2..]}d00h00min");
                            }
                            else if (drCode.StartsWith("DRH"))
                            {
                                Console.WriteLine($"时间步长码：00d{drCode[^2..]}h00min");
                            }
                            else if (drCode.StartsWith("DRN"))
                            {
                                Console.WriteLine($"时间步长码：00d00h{drCode[^2..]}min");
                            }

                            (string name, string unit) a = ("", "");
                            (int d, decimal pre) vl = (0, 0);
                            for (int i = 6; i < arr.Length - 1; i++)
                            {
                                var elemCode = arr[i];
                                if (arr[i] == "DRP")
                                {
                                    var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[i]);

                                    var (name, unit) = ElementInfo.Instance.GetInfo(arr[i]);
                                    Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name}");
                                    for (int j = 0; j < 12; j++)
                                    {
                                        var d1 = arr[i + 1];
                                        var v = Convert.ToDecimal(Convert.ToUInt16(d1, 16)) / 10;
                                        if (Regex.IsMatch(d1, "^[Ff]*$"))
                                        {
                                            v = 0;
                                        }
                                        Console.WriteLine($"数据：{v}{unit}");

                                        i += 1;
                                    }
                                    continue;
                                }

                                if (arr[i].StartsWith("DRZ"))
                                {
                                    var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[i]);

                                    var (name, unit) = ElementInfo.Instance.GetInfo(arr[i]);
                                    Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name}");
                                    for (int j = 0; j < 12; j++)
                                    {
                                        var d1 = arr[i + 1];
                                        var v = Convert.ToDecimal(Convert.ToUInt16(d1, 16)) / 100;
                                        if (Regex.IsMatch(d1, "^[Ff]*$"))
                                        {
                                            v = 0;
                                        }
                                        Console.WriteLine($"数据：{v}{unit}");

                                        i += 1;

                                    }
                                    continue;
                                }

                                if (Enum.IsDefined(typeof(ElementCode), arr[i]))
                                {
                                    var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[i]);

                                    vl = ElementInfo.GetValueType(elementCode);
                                    a = ElementInfo.Instance.GetInfo(elementCode);
                                    Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{a.name}");

                                }
                                else
                                {
                                    var d1 = arr[i];
                                    decimal d = Convert.ToDecimal(Helper.HEXToBCDInt(d1));
                                    
                                    if (vl.d != 0)
                                    {
                                        d /= vl.pre;
                                    }
                                    Console.WriteLine($"数据：{d}{a.unit}");
                                }
                            }

                            break;
                        }
                    case FunctionCode.FC_35:
                    case FunctionCode.FC_39:
                        //人工置数报
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            if (Enum.IsDefined(typeof(ElementCode), arr[0]))
                            {
                                var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), arr[0]);

                                var (name, unit) = ElementInfo.Instance.GetInfo(elementCode);
                                Console.WriteLine($"要素标识符:{Convert.ToString((byte)elementCode, 16)} {elementCode},名称：{name}");

                                for (int i = 1; i < arr.Length - 1; i += 2)
                                {
                                    var (n, un) = ManualEntryInfo.GetInfo(arr[i]);
                                    Console.WriteLine($"名称：{n},数据：{arr[i + 1]}{un}");
                                }

                            }

                            break;
                        }
                    case FunctionCode.FC_36:
                        //图片
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var typestr = arr[2];
                            if (Enum.IsDefined(typeof(TelemetryStationCode), typestr))
                            {
                                var typecode = (TelemetryStationCode)Enum.Parse(typeof(TelemetryStationCode), typestr);
                                var typename = TelemetryStationInfo.GetInfo(typecode);
                                Console.WriteLine($"遥测站分类码:{Helper.ToHEXString((byte)typecode)}-{typestr}-{typename}");
                            }

                            var tt = arr[4];
                            var tString = $"{tt[0..2]}年{tt[2..4]}月{tt[4..6]}日{tt[6..8]}:{tt[8..10]}";
                            Console.WriteLine($"观测时间:{tString}");

                            var elemCode = arr[5];

                            if (Enum.IsDefined(typeof(ElementCode), elemCode))
                            {
                                var elementCode = (ElementCode)Enum.Parse(typeof(ElementCode), elemCode);
                                var (name, unit) = ElementInfo.Instance.GetInfo(elemCode);

                                Console.WriteLine(name);

                                //图片信息
                                if (elementCode == ElementCode.PIC)
                                {
                                    List<byte> img = new List<byte>();
                                    while (byteBlock.Position != byteBlock.Length - 5)
                                    {
                                        var sa = byteBlock.ReadByte();
                                        img.Add(sa);
                                    }
                                    Console.WriteLine(Helper.ByteToImg(img.ToArray()));
                                }
                            }

                        }
                        break;
                    case FunctionCode.FC_40:
                    case FunctionCode.FC_41:
                        //基本配置表
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            for (int i = 2; i < arr.Length - 1; i++)
                            {
                                var code = arr[i];
                                switch (code)
                                {
                                    //中心站地址
                                    case "01":
                                        {
                                            var centers = arr[++i];
                                            Console.WriteLine($"中心站1地址：{centers[..2]}");
                                            Console.WriteLine($"中心站2地址：{centers[2..4]}");
                                            Console.WriteLine($"中心站3地址：{centers[4..6]}");
                                            Console.WriteLine($"中心站4地址：{centers[6..]}");
                                        }
                                        break;
                                    //遥测站地址
                                    case "02":
                                        {
                                            Console.WriteLine($"遥测站地址:{arr[++i]}");
                                            break;
                                        }
                                    //密码
                                    case "03":
                                        {
                                            Console.WriteLine($"密码:{arr[++i]}");
                                            break;
                                        }
                                    //中心站1主信道类型及地址
                                    case "04":
                                        {
                                            Console.WriteLine("中心站1主信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //中心站1备用信道类型及地址
                                    case "05":
                                        {
                                            Console.WriteLine("中心站1备用信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //中心站2主信道类型及地址
                                    case "06":
                                        {
                                            Console.WriteLine("中心站2主信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //中心站2备用信道类型及地址
                                    case "07":
                                        {
                                            Console.WriteLine("中心站2备用信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //中心站3主信道类型及地址
                                    case "08":
                                        {
                                            Console.WriteLine("中心站3主信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //中心站3备用信道类型及地址
                                    case "09":
                                        {
                                            Console.WriteLine("中心站3备用信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //中心站4主信道类型及地址
                                    case "0A":
                                        {
                                            Console.WriteLine("中心站4主信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //中心站4备用信道类型及地址
                                    case "0B":
                                        {
                                            Console.WriteLine("中心站4备用信道类型及地址：");
                                            var d = arr[++i];
                                            var xindao = d[..2];
                                            var xin = Convert.ToInt16(xindao, 16);
                                            var addr = d[2..];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信:{addr}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4:{addr}");
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗:{addr}");
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星:{addr}");
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN:{addr}");
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波:{addr}");
                                            }
                                            break;
                                        }
                                    //工作方式
                                    case "0C":
                                        {
                                            var workType = arr[++i];
                                            var wt = Convert.ToInt16(workType, 16);
                                            if (wt == 1)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-自报工作状态");
                                            }
                                            else if (wt == 2)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-自报确认工作状态");
                                            }
                                            else if (wt == 3)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-查询/应答工作状态");
                                            }
                                            else if (wt == 4)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-调试或维修状态");
                                            }
                                            break;
                                        }
                                    //遥测站采集要素设置
                                    case "0D":
                                        {
                                            var d = arr[++i];
                                            var a1 = d[..2];
                                            var a1byte = byte.Parse(a1, NumberStyles.HexNumber);
                                            var a1bin = Helper.ByteToBinString(a1byte);
                                            var a1str = $"降水量：{a1bin[0]}，蒸发量：{a1bin[1]}，风向：{a1bin[2]}，风速：{a1bin[3]}，气温：{a1bin[4]}，湿度：{a1bin[5]}，地温：{a1bin[6]}，气压：{a1bin[7]}";
                                            Console.WriteLine(a1str);

                                            var a2 = d[2..4];
                                            var a2byte = byte.Parse(a2, NumberStyles.HexNumber);
                                            var a2bin = Helper.ByteToBinString(a2byte);
                                            var a2str = $"水位8：{a2bin[0]}，水位7：{a2bin[1]}，水位6：{a2bin[2]}，水位5：{a2bin[3]}，水位4：{a2bin[4]}，水位3：{a2bin[5]}，水位2：{a2bin[6]}，水位1：{a2bin[7]}";
                                            Console.WriteLine(a2str);

                                            var a3 = d[4..6];
                                            var a3byte = byte.Parse(a3, NumberStyles.HexNumber);
                                            var a3bin = Helper.ByteToBinString(a3byte);
                                            var a3str = $"地下水埋深：{a3bin[0]}，图片：{a3bin[1]}，波浪：{a3bin[2]}，闸门开度：{a3bin[3]}，水量：{a3bin[4]}，流速：{a3bin[5]}，流量：{a3bin[6]}，水压：{a3bin[7]}";
                                            Console.WriteLine(a3str);

                                            var a4 = d[6..8];
                                            var a4byte = byte.Parse(a4, NumberStyles.HexNumber);
                                            var a4bin = Helper.ByteToBinString(a4byte);
                                            var a4str = $"水表8：{a4bin[0]}，水表7：{a4bin[1]}，水表6：{a4bin[2]}，水表5：{a4bin[3]}，水表4：{a4bin[4]}，水表3：{a4bin[5]}，水表2：{a4bin[6]}，水表1：{a4bin[7]}";
                                            Console.WriteLine(a4str);

                                            var a5 = d[8..10];
                                            var a5byte = byte.Parse(a5, NumberStyles.HexNumber);
                                            var a5bin = Helper.ByteToBinString(a5byte);
                                            var a5str = $"100厘米墒情：{a5bin[0]}，80厘米墒情：{a5bin[1]}，60厘米墒情：{a5bin[2]}，50厘米墒情：{a5bin[3]}，40厘米墒情：{a5bin[4]}，30厘米墒情：{a5bin[5]}，20厘米墒情：{a5bin[6]}，10厘米墒情：{a5bin[7]}";
                                            Console.WriteLine(a5str);

                                            var a6 = d[10..12];
                                            var a6byte = byte.Parse(a6, NumberStyles.HexNumber);
                                            var a6bin = Helper.ByteToBinString(a6byte);
                                            var a6str = $"pH值：{a6bin[0]}，溶解氧：{a6bin[1]}，电导率：{a6bin[2]}，浊度：{a6bin[3]}，氧化还原电位：{a6bin[4]}，高猛酸盐指数：{a6bin[5]}，氨氮：{a6bin[6]}，水温：{a5bin[7]}";
                                            Console.WriteLine(a6str);

                                            var a7 = d[12..14];
                                            var a7byte = byte.Parse(a7, NumberStyles.HexNumber);
                                            var a7bin = Helper.ByteToBinString(a7byte);
                                            var a7str = $"总有机碳：{a7bin[0]}，总氮：{a7bin[1]}，总磷：{a7bin[2]}，锌：{a7bin[3]}，硒：{a7bin[4]}，砷：{a7bin[5]}，总汞：{a7bin[6]}，镉：{a7bin[7]}";
                                            Console.WriteLine(a7str);

                                            var a8 = d[14..];
                                            var a8byte = byte.Parse(a8, NumberStyles.HexNumber);
                                            var a8bin = Helper.ByteToBinString(a8byte);
                                            var a8str = $"{a8bin[0]}，{a8bin[1]}，{a8bin[2]}，{a8bin[3]}，{a8bin[4]}，叶绿素a：{a8bin[5]}，铜：{a8bin[6]}，铅：{a8bin[7]}";
                                            Console.WriteLine(a8str);

                                            break;
                                        }
                                    //中继站(集合转发站)服务地址范围
                                    case "0E":
                                        {
                                            var d = arr[++i];
                                            Console.WriteLine($"中继站(集合转发站)服务地址范围:{d}");
                                            break;
                                        }
                                    //遥测站通信设备识别号
                                    case "0F":
                                        {
                                            var d = arr[++i];

                                            var sim = d[..2];
                                            //var st = Convert.ToInt16(sim, 16);
                                            var st = Convert.ToInt16(sim);
                                            if (st == 1)
                                            {
                                                Console.WriteLine($"卡类型：{st}-移动通信卡,卡识别号：{d[2..]}");
                                            }
                                            else if (st == 2)
                                            {
                                                Console.WriteLine($"卡类型：{st}-北斗卫星通信卡,卡识别号：{d[2..]}");
                                            }
                                            else
                                            {
                                                Console.WriteLine($"遥测站通信设备识别号:{d}");
                                            }
                                            break;
                                        }
                                    default:
                                        break;
                                }
                            }
                        }
                        break;
                    case FunctionCode.FC_42:
                    case FunctionCode.FC_43:
                        //运行参数表
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            for (int i = 2; i < arr.Length - 1; i++)
                            {
                                //读取一个标识符
                                var elemCode = arr[i];
                                var ec = byte.Parse(elemCode, NumberStyles.HexNumber);
                                var (name, unit) = RunConfigInfo.Instance.GetInfo((RunConfigCode)ec);
                                var d1 = arr[++i];
                                Console.WriteLine($"运行参数标识符:{elemCode},名称:{name},数据:{d1}{unit}");
                            }
                        }
                        break;
                    case FunctionCode.FC_45:
                        //软件版本
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            Console.WriteLine(arr[2]);

                            break;
                        }
                    case FunctionCode.FC_46:
                    case FunctionCode.FC_4B:
                        //遥测站状态和报警信息
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            if (arr[2] == "ZT")
                            {
                                var d = arr[3];
                                var a1byte = Helper.HEXStrToByte(d);
                                var binStr = Helper.ByteToBinString(a1byte);
                                var str = binStr.ToCharArray();
                                Array.Reverse(str);
                                Console.WriteLine(binStr);

                                #region 报警状态解析
                                if (str[0] == '0')
                                {
                                    Console.WriteLine($"交流电充电状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("交流电充电状态：停电");
                                }
                                if (str[1] == '0')
                                {
                                    Console.WriteLine("蓄电池电压状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("蓄电池电压状态：电压低");
                                }
                                if (str[2] == '0')
                                {
                                    Console.WriteLine("水位超限报警状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("水位超限报警状态：报警");
                                }
                                if (str[3] == '0')
                                {
                                    Console.WriteLine("流量超限报警状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("流量超限报警状态：报警");
                                }
                                if (str[4] == '0')
                                {
                                    Console.WriteLine("水质超限报警状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("水质超限报警状态：报警");
                                }
                                if (str[5] == '0')
                                {
                                    Console.WriteLine("流量仪表状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("流量仪表状态：故障");
                                }
                                if (str[6] == '0')
                                {
                                    Console.WriteLine("水位仪表状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("水位仪表状态：故障");
                                }
                                if (str[7] == '0')
                                {
                                    Console.WriteLine("终端箱门状态：开启");
                                }
                                else
                                {
                                    Console.WriteLine("终端箱门状态：关闭");
                                }
                                if (str[8] == '0')
                                {
                                    Console.WriteLine("存储器状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("存储器状态：异常");
                                }
                                if (str[9] == '0')
                                {
                                    Console.WriteLine("IC卡功能有效：关闭");
                                }
                                else
                                {
                                    Console.WriteLine("IC卡功能有效：IC卡有效");
                                }
                                if (str[10] == '0')
                                {
                                    Console.WriteLine("水泵工作状态：水泵工作");
                                }
                                else
                                {
                                    Console.WriteLine("水泵工作状态：水泵停机");
                                }
                                if (str[11] == '0')
                                {
                                    Console.WriteLine("剩余水量报警：未超限");
                                }
                                else
                                {
                                    Console.WriteLine("剩余水量报警：水量超限");
                                }
                                #endregion

                            }
                            break;
                        }
                    case FunctionCode.FC_47://初始化固态存储数据
                    case FunctionCode.FC_48://恢复出厂设置
                    case FunctionCode.FC_4A://设置遥测站时钟
                    case FunctionCode.FC_51://查询遥测站时钟
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");
                            break;
                        }
                    case FunctionCode.FC_49:
                        //修改密码
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var pwd = arr[3];
                            Console.WriteLine($"新密码:{pwd}");

                            break;
                        }
                    case FunctionCode.FC_4C:
                        //水泵状态
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var l = arr[2];
                            var bl = l[2..];
                            var blb = byte.Parse(bl, NumberStyles.HexNumber);
                            var bs = Helper.ByteToBinString(blb);
                            var str = bs.ToCharArray();
                            Array.Reverse(str);
                            for (int i = 0; i < str.Length; i++)
                            {
                                string t = str[i] == '0' ? "关闭" : "开启";
                                Console.Write($"{i + 1}号水泵状态：{str[i]}-{t}");
                            }
                            break;
                        }
                    case FunctionCode.FC_4D:
                        //阀门状态
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var l1 = arr[2];
                            var bl1 = l1[2..];
                            var blb1 = byte.Parse(bl1, NumberStyles.HexNumber);
                            var bs1 = Helper.ByteToBinString(blb1);
                            var str = bs1.ToCharArray();
                            Array.Reverse(str);
                            for (int i = 0; i < str.Length; i++)
                            {
                                string t = str[i] == '0' ? "关闭" : "开启";
                                Console.Write($"{i + 1}号阀门状态：{str[i]}-{t}");
                            }
                            break;
                        }
                    case FunctionCode.FC_4E:
                        //闸门状态
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var jl = arr[2];
                            var jm = jl[2..];
                            var jmb = byte.Parse(jm, NumberStyles.HexNumber);
                            var jms = Helper.ByteToBinString(jmb);
                            var str = jms.ToCharArray();
                            Array.Reverse(str);
                            for (int i = 0; i < str.Length; i++)
                            {
                                string t = str[i] == '0' ? "关闭" : "开启";
                                Console.Write($"{i + 1}号闸门状态：{str[i]}-{t}");
                            }

                            for (int i = 3; i < arr.Length - 1; i++)
                            {
                                var kd = arr[i];
                                var kdb = byte.Parse(kd);
                                var v = Helper.ByteToBCDInt(kdb);
                                Console.WriteLine($"{i + 1}号闸门开度：{v}cm");
                            }

                            break;
                        }
                    case FunctionCode.FC_4F:
                        //水量定值
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            var cz = arr[2];
                            if (cz == "FF")
                            {
                                Console.WriteLine("定值控制投入");
                            }
                            else if (cz == "00")
                            {
                                Console.WriteLine("定值控制退出");
                            }
                            break;
                        }
                    case FunctionCode.FC_50:
                        //遥测站事件记录
                        {
                            var datab = byteBlock.ToArray(byteBlock.Position);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            Console.WriteLine($"遥测站地址:{arr[1]}");

                            ReadOnlyMemory<char> cs = arr[2].AsMemory();
                            for (int i = 1, j = 0; i < 33; i++, j += 4)
                            {
                                var num = Convert.ToUInt16(cs.Slice(j, 4).ToString(), 16);
                                switch (i)
                                {
                                    case 1:
                                        //var num1 = Convert.ToUInt16(cs.Slice(j, 4).ToString(),16);
                                        Console.WriteLine($"ERC{i}-历史数据初始化记录：{num}");
                                        break;
                                    case 2:
                                        //var num2 = arr[j];
                                        Console.WriteLine($"ERC{i}-参数变更记录：{num}");
                                        break;
                                    case 3:
                                        //var num3 = arr[j];
                                        Console.WriteLine($"ERC{i}-状态量变位记录：{num}");
                                        break;
                                    case 4:
                                        //var num4 = arr[j];
                                        Console.WriteLine($"ERC{i}-传感器及仪表故障记录：{num}");
                                        break;
                                    case 5:
                                        //var num5 = arr[j];
                                        Console.WriteLine($"ERC{i}-密码修改记录：{num}");
                                        break;
                                    case 6:
                                        //var num6 = arr[j];
                                        Console.WriteLine($"ERC{i}-终端故障记录：{num}");
                                        break;
                                    case 7:
                                        //var num7 = arr[j];
                                        Console.WriteLine($"ERC{i}-交流失电记录：{num}");
                                        break;
                                    case 8:
                                        //var num8 = arr[j];
                                        Console.WriteLine($"ERC{i}-蓄电池电压低告警记录：{num}");
                                        break;
                                    case 9:
                                        //var num9 = arr[j];
                                        Console.WriteLine($"ERC{i}-终端箱非法打开记录：{num}");
                                        break;
                                    case 10:
                                        //var num10 = arr[j];
                                        Console.WriteLine($"ERC{i}-水泵故障记录：{num}");
                                        break;
                                    case 11:
                                        //var num11 = arr[j];
                                        Console.WriteLine($"ERC{i}-剩余水量超限告警记录：{num}");
                                        break;
                                    case 12:
                                        //var num12 = arr[j];
                                        Console.WriteLine($"ERC{i}-水位超限告警记录：{num}");
                                        break;
                                    case 13:
                                        //var num13 = arr[j];
                                        Console.WriteLine($"ERC{i}-水压超限告警记录：{num}");
                                        break;
                                    case 14:
                                        //var num14 = arr[j];
                                        Console.WriteLine($"ERC{i}-水质参数超限告警记录：{num}");
                                        break;
                                    case 15:
                                        //var num15 = arr[j];
                                        Console.WriteLine($"ERC{i}-数据出错记录：{num}");
                                        break;
                                    case 16:
                                        //var num16 = arr[j];
                                        Console.WriteLine($"ERC{i}-发报文记录：{num}");
                                        break;
                                    case 17:
                                        //var num17 = arr[j];
                                        Console.WriteLine($"ERC{i}-收报文记录：{num}");
                                        break;
                                    case 18:
                                        //var num18 = arr[j];
                                        Console.WriteLine($"ERC{i}-发报文出错记录：{num}");
                                        break;
                                    default:
                                        //var num19 = arr[j];
                                        Console.WriteLine($"ERC{i}-：{num}");
                                        break;
                                }
                            }

                            break;
                        }
                    default:
                        break;

                }

                //如果不是链路维持报则发送响应报文
                if (header.FunctionCode != FunctionCode.FC_2F)
                {
                    var send = BuildResponse.CreatResponse(header, serialnum, EndMarks.ESC);

                    var shex = Encoding.ASCII.GetString(send);
                    Console.WriteLine($"\n发送ASCII：{shex}");

                    await sessionClient.SendAsync(send);
                }

            }
            else if (byteBlock.Span.StartsWith(MessageFrame.HEX_BCD))
            {
                Console.WriteLine($"报文长度：{byteBlock.Length}，正文长度{byteBlock.Length - 14 - 3}");

                MessageFrame header = new MessageFrame();
                header.SetFrameCode(byteBlock.ReadToSpan(2).ToArray());
                var s = BitConverter.ToUInt16(header.FrameCoding);
                Console.WriteLine(Convert.ToString(s, 16));

                //计算并检查校验码
                if (header.CheckCRC(byteBlock))
                {
                    Console.WriteLine("校验码正确");
                }
                else
                {
                    Console.WriteLine("校验码错误");
                }

                #region 报头报尾读取
                //var ccode = byteBlock.ReadByte();
                //Console.WriteLine($"中心站地址:{ccode}");

                /*var f = byteBlock.ReadToArray(3);
                var f2 = byteBlock.ReadToArray(2);
                var b = Convert.ToUInt16(Helper.ToHEXString(f2), 16);
                var siteCode = Helper.ToHEXString(f) + (b.ToString().PadLeft(4, '0'));*/

                /*var f = byteBlock.ReadToArray(5);
                var siteCode = Helper.ToHEXString(f);
                Console.WriteLine($"遥测站地址:{siteCode}");

                var pwdh = byteBlock.ReadToArray(2);
                var pwd1 = Convert.ToUInt16(Helper.ToHEXString(pwdh), 16);
                Console.WriteLine($"密码:{pwd1}");

                var fccode = byteBlock.ReadToArray(1);
                var fn = FunctionCodeInfo.GetName((FunctionCode)fccode[0]);
                Console.WriteLine($"{fn},功能码:{Helper.ToHEXString(fccode)}");

                var u = Helper.ToHEXString(byteBlock.ReadToArray(2));
                var c1 = u[..1];
                Console.WriteLine($"上下行标识:{Convert.ToInt32(c1, 16)}上行");

                var len = Convert.ToInt32(u[1..], 16);
                var len1 = Convert.ToInt32(u, 16);
                Console.WriteLine($"报文长度:{len}");

                Console.WriteLine($"报文起始符:{Helper.ToHEXString(byteBlock.ReadToArray(1))}");

                var o = byteBlock.ToArray(byteBlock.Len - 3, 1);
                var o1 = byteBlock[byteBlock.Len - 3];
                Console.WriteLine($"报文结束符:{Helper.ToHEXString(o)}");*/
                #endregion

                header.ReadHead(byteBlock);
                await sessionClient.ResetIdAsync(header.TelemetryAddressStr);

                Console.WriteLine($"中心站地址:{header.CentralAddressStr}");

                Console.WriteLine($"遥测站地址:{header.TelemetryAddressStr}");

                Console.WriteLine($"密码:{header.PassWordStr}");

                var fn = FunctionCodeInfo.GetName(header.FunctionCode);
                Console.WriteLine($"{fn},功能码:{Helper.ToHEXString((byte)header.FunctionCode)}");

                Console.WriteLine($"报文长度:{header.MessageLength}");

                Console.WriteLine($"报文起始符:{header.StartMark}-{(byte)header.StartMark}");
                Console.WriteLine($"报文结束符:{header.EndMark}-{(byte)header.EndMark}");

                Console.WriteLine($"---正文内容---");

                //ReadHEXData.Instance.ReadHEXMessage(byteBlock, header);
                //var messageData = ReadHEXData.Instance.Message;
                //return Task.CompletedTask;


                //多包传输
                if (header.StartMark == StartMarks.SYN)
                {
                    Console.WriteLine($"包总数及序列号:{header.PackageIndex}/{header.PackageCount}");
                    var bytes = byteBlock.ToArray(byteBlock.Position, header.MessageLength);
                    SYN.Write(bytes);

                    if (header.PackageIndex == header.PackageCount)
                    {
                        var block = SYN.Block;
                        //解析正文
                        ReadHEXData.Instance.ReadHEXMessage(block, header.FunctionCode);
                        var messageData = ReadHEXData.Instance.Message;
                        SYN.Clera();

                    }

                    e.Handled = true;//表示该数据已经被本插件处理，无需再投递到其他插件。
                    return;
                }

                //读取流水号
                byte[] serialnum = new byte[2];
                byteBlock.Read(serialnum);
                var snum = Helper.ToHEXString(serialnum);
                Console.WriteLine($"流水号:{Convert.ToUInt16(snum, 16)}-{snum}");

                //读取发报时间
                byte[] timeb = new byte[6];
                byteBlock.Read(timeb);
                var timeHEX = BitConverter.ToString(timeb);
                var timearr = timeHEX.Split('-');
                var timeString = $"{timearr[0]}年{timearr[1]}月{timearr[2]}日{timearr[3]}:{timearr[4]}:{timearr[5]}";
                Console.WriteLine($"发报时间:{timeString}");


                //根据不同的功能码对正文进行解析
                switch (header.FunctionCode)
                {
                    case FunctionCode.FC_30:
                    case FunctionCode.FC_32:
                    case FunctionCode.FC_33:
                    case FunctionCode.FC_34://小时报
                    case FunctionCode.FC_37:
                    case FunctionCode.FC_3A:
                    case FunctionCode.FC_44:
                        //要素数据
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                byte[] f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var typecode = byteBlock.ReadByte();
                            var typename = TelemetryStationInfo.GetInfo((TelemetryStationCode)typecode);
                            Console.WriteLine($"遥测站分类码:{Helper.ToHEXString(typecode)}-{(TelemetryStationCode)typecode}-{typename}");

                            var tt = byteBlock.ReadByte();
                            if (tt == 0xf0)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var tb = new byte[5];
                                byteBlock.Read(tb);
                                var tHEX = BitConverter.ToString(tb);
                                var tarr = tHEX.Split('-');
                                var tString = $"{tarr[0]}年{tarr[1]}月{tarr[2]}日{tarr[3]}:{tarr[4]}";
                                Console.WriteLine($"观测时间:{tString}");
                            }
                            else
                            {
                                Console.WriteLine("观测时间引导符错误");
                            }

                            //读取一个标识符
                            var elemCode = byteBlock.ReadByte();
                            while (elemCode != 0x03 && elemCode != 0x17)
                            {
                                //跳过数据定义
                                //byteBlock.Seek(1, SeekOrigin.Current);
                                //读取数据定义
                                var dlen = byteBlock.ReadByte();
                                var dalen = dlen >> 3;
                                var dalen1 = dlen & 0x07;
                                //Console.WriteLine($"数据定义:{dlen},字节数:{dalen},小数位数:{dalen1}");
                                var (d, pre) = ElementInfo.GetValueType((ElementCode)elemCode);
                                var (name, unit) = ElementInfo.Instance.GetInfo((ElementCode)elemCode);

                                //var def = Helper.GetDataDefinitionBytes((d, pre));
                                //Console.WriteLine($"数据定义：{Helper.ToHEXString(def)}");


                                if (d != 0)
                                {
                                    switch ((ElementCode)elemCode)
                                    {
                                        //1小时内每5分钟时段降雨量
                                        case ElementCode.DRP:
                                            { //var d1 = Helper.ToHEXString(byteBlock.ReadToArray(12));
                                                decimal[] drp = new decimal[12];
                                                Console.WriteLine($"要素标识符:{Convert.ToString(elemCode, 16)} {(ElementCode)elemCode},名称：{name}");
                                                for (int i = 0; i < 12; i++)
                                                {
                                                    decimal v = Convert.ToDecimal(byteBlock.ReadByte()) / 10;
                                                    if (v == 25.5M)
                                                    {
                                                        v = 0;
                                                    }
                                                    drp[i] = v;
                                                    Console.WriteLine($"{v}{unit}");
                                                }
                                                break;
                                            }
                                        case ElementCode.DRZ1:
                                        case ElementCode.DRZ2:
                                        case ElementCode.DRZ3:
                                        case ElementCode.DRZ4:
                                        case ElementCode.DRZ5:
                                        case ElementCode.DRZ6:
                                        case ElementCode.DRZ7:
                                        case ElementCode.DRZ8:
                                            {//var d2 = Helper.ToHEXString(byteBlock.ReadToArray(24));
                                                decimal[] drz = new decimal[12];
                                                Console.WriteLine($"要素标识符:{Convert.ToString(elemCode, 16)} {(ElementCode)elemCode},名称：{name}");
                                                for (int i = 0; i < 12; i++)
                                                {
                                                    decimal v = Convert.ToDecimal(byteBlock.ReadUInt16(EndianType.Big)) / 100;
                                                    if (v == 655.35M)
                                                    {
                                                        v = 0;
                                                    }
                                                    drz[i] = v;
                                                    Console.WriteLine($"{v}{unit}");
                                                }
                                                break;
                                            }
                                        default:
                                            {
                                                var d1b = new byte[d];
                                                byteBlock.Read(d1b);
                                                var d1 = Helper.ToHEXString(d1b);
                                                //var num1 = Helper.HEXToBCDInt(d1);
                                                var num1 = Convert.ToDecimal(d1);
                                                decimal value;
                                                if (Regex.IsMatch(d1, "^[Ff]*$"))
                                                {
                                                    value = 0;
                                                }
                                                else if (pre != 0)
                                                {
                                                    value = num1 / pre;
                                                }
                                                else
                                                {
                                                    value = Convert.ToDecimal(num1);
                                                }
                                                Console.WriteLine($"要素标识符:{Convert.ToString(elemCode, 16)} {(ElementCode)elemCode},名称:{name},数据:{value}{unit}");
                                                break;
                                            }
                                    }
                                }

                                //读取下一个要素标识符
                                elemCode = byteBlock.ReadByte();
                            }
                            break;
                        }
                    case FunctionCode.FC_31://有时间步长码
                    case FunctionCode.FC_38://有时间步长码
                        //均匀时段报
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);

                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var typecode = byteBlock.ReadByte();
                            var typename = TelemetryStationInfo.GetInfo((TelemetryStationCode)typecode);
                            Console.WriteLine($"遥测站分类码:{Helper.ToHEXString(typecode)}-{(TelemetryStationCode)typecode}-{typename}");

                            var tt = byteBlock.ReadByte();
                            if (tt == 0xf0)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var tb = new byte[5];
                                byteBlock.Read(tb);
                                var tHEX = BitConverter.ToString(tb);
                                var tarr = tHEX.Split('-');
                                var tString = $"{tarr[0]}年{tarr[1]}月{tarr[2]}日{tarr[3]}:{tarr[4]}";
                                Console.WriteLine($"观测时间:{tString}");
                            }
                            else
                            {
                                Console.WriteLine("观测时间引导符错误");
                            }

                            //时间步长码
                            var drCode = byteBlock.ReadByte();
                            //跳过数据定义
                            byteBlock.Seek(1, SeekOrigin.Current);

                            var drnb = new byte[3];
                            byteBlock.Read(drnb);
                            var dr = BitConverter.ToString(drnb);
                            var darr = dr.Split('-');
                            Console.WriteLine($"时间步长码：{darr[0]}d{darr[1]}h{darr[2]}min");

                            //读取一个标识符
                            var elemCode = byteBlock.ReadByte();
                            //读取数据定义
                            var dlen = byteBlock.ReadByte();
                            var dalen = dlen >> 3;
                            var dalen1 = dlen & 0x07;
                            //Console.WriteLine($"数据定义:{dlen},字节数:{dalen},小数位数:{dalen1}");

                            var (d, pre) = ElementInfo.GetValueType((ElementCode)elemCode);
                            var (name, unit) = ElementInfo.Instance.GetInfo((ElementCode)elemCode);

                            Console.WriteLine($"要素标识符：{Helper.ToHEXString(elemCode)} {(ElementCode)elemCode}，名称：{name}");
                            string regex = "^[Ff]*$";

                            while (byteBlock.Position < byteBlock.Length - 3)
                            {
                                var va = new byte[d];
                                byteBlock.Read(va);
                                var d1 = Helper.ToHEXString(va);
                                int num1;
                                if (elemCode >= 0xF4 && elemCode <= 0xFC)
                                {
                                    num1 = Convert.ToUInt16(d1, 16);
                                }
                                else
                                {
                                    num1 = Helper.HEXToBCDInt(d1);
                                }
                                decimal value;
                                if (Regex.IsMatch(d1, regex))
                                {
                                    value = 0;
                                }
                                else if (pre != 0)
                                {
                                    value = num1 / pre;
                                }
                                else
                                {
                                    value = Convert.ToDecimal(num1);
                                }
                                Console.WriteLine($"{value}{unit}");

                            }
                            break;
                        }
                    case FunctionCode.FC_35:
                    case FunctionCode.FC_39:
                        //人工置数报
                        {
                            var tt1 = byteBlock.ReadByte();
                            if (tt1 == 0xF2)
                            {
                                Console.WriteLine("人工置数");
                            }
                            byteBlock.ReadByte();

                            int con = header.MessageLength - 10;
                            var datab = byteBlock.ToArray(byteBlock.Position, con);
                            var datastr = datab.ToASCIIString();
                            Console.WriteLine(datastr);

                            var arr = datastr.Split(" ");
                            for (int i = 0; i < arr.Length - 1; i += 2)
                            {
                                var (n, un) = ManualEntryInfo.GetInfo(arr[i]);
                                Console.WriteLine($"名称：{n},数据：{arr[i + 1]}{un}");
                            }

                            break;
                        }
                    case FunctionCode.FC_36:
                        //图片
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var typecode = byteBlock.ReadByte();
                            var typename = TelemetryStationInfo.GetInfo((TelemetryStationCode)typecode);
                            Console.WriteLine($"遥测站分类码:{Helper.ToHEXString(typecode)}-{(TelemetryStationCode)typecode}-{typename}");

                            var tt2 = byteBlock.ReadByte();
                            if (tt2 == 0xf0)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var tb = new byte[5];
                                byteBlock.Read(tb);
                                var tHEX = BitConverter.ToString(tb);
                                var tarr = tHEX.Split('-');
                                var tString = $"{tarr[0]}年{tarr[1]}月{tarr[2]}日{tarr[3]}:{tarr[4]}";
                                Console.WriteLine($"观测时间:{tString}");
                            }
                            else
                            {
                                Console.WriteLine("观测时间引导符错误");
                            }

                            var elemCode = byteBlock.ReadByte();

                            if (elemCode != 0x03 && elemCode != 0x17)
                            {
                                //跳过数据定义
                                byteBlock.Seek(1, SeekOrigin.Current);

                                var (name, unit) = ElementInfo.Instance.GetInfo((ElementCode)elemCode);

                                Console.WriteLine(name);

                                //图片信息
                                if ((ElementCode)elemCode == ElementCode.PIC)
                                {
                                    List<byte> img = new List<byte>();
                                    while (byteBlock.Position != byteBlock.Length - 5)
                                    {
                                        var sa = byteBlock.ReadByte();
                                        img.Add(sa);
                                    }
                                    Console.WriteLine(Helper.ByteToImg(img.ToArray()));
                                }
                            }

                        }
                        break;
                    case FunctionCode.FC_40:
                    case FunctionCode.FC_41:
                        //基本配置表
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);

                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            //读取一个标识符
                            var elemCode = byteBlock.ReadByte();
                            while (byteBlock.Position < byteBlock.Length - 3)
                            {
                                //读取数据定义
                                var dlen = byteBlock.ReadByte();
                                var dalen = dlen >> 3;
                                var dalen1 = dlen & 0x07;
                                //Console.WriteLine($"数据定义:{dlen},字节数:{dalen},小数位数:{dalen1}");
                                switch (elemCode)
                                {
                                    //中心站地址
                                    case 0x01:
                                        {
                                            var centers = new byte[4];
                                            byteBlock.Read(centers);
                                            Console.WriteLine($"中心站1地址：{centers[0]}");
                                            Console.WriteLine($"中心站2地址：{centers[1]}");
                                            Console.WriteLine($"中心站3地址：{centers[2]}");
                                            Console.WriteLine($"中心站4地址：{centers[3]}");
                                        }
                                        break;
                                    //遥测站地址
                                    case 0x02:
                                        {
                                            var f = new byte[5];
                                            byteBlock.Read(f);
                                            var siteCode1 = Helper.ToHEXString(f);
                                            Console.WriteLine($"遥测站地址:{siteCode1}");
                                            break;
                                        }
                                    //密码
                                    case 0x03:
                                        {
                                            var pwd = new byte[2];
                                            byteBlock.Read(pwd);
                                            Console.WriteLine($"密码:{Helper.ToHEXString(pwd)}");
                                            break;
                                        }
                                    //中心站1主信道类型及地址
                                    case 0x04:
                                        {
                                            Console.WriteLine("中心站1主信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //中心站1备用信道类型及地址
                                    case 0x05:
                                        {
                                            Console.WriteLine("中心站1备用信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //中心站2主信道类型及地址
                                    case 0x06:
                                        {
                                            Console.WriteLine("中心站2主信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //中心站2备用信道类型及地址
                                    case 0x07:
                                        {
                                            Console.WriteLine("中心站2备用信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //中心站3主信道类型及地址
                                    case 0x08:
                                        {
                                            Console.WriteLine("中心站3主信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //中心站3备用信道类型及地址
                                    case 0x09:
                                        {
                                            Console.WriteLine("中心站3备用信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //中心站4主信道类型及地址
                                    case 0x0A:
                                        {
                                            Console.WriteLine("中心站4主信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //中心站4备用信道类型及地址
                                    case 0x0B:
                                        {
                                            Console.WriteLine("中心站4备用信道类型及地址：");
                                            var xindao = byteBlock.ReadByte();
                                            var xin = Helper.ByteToBCDInt(xindao);
                                            //地址
                                            var addr = new byte[dalen - 1];
                                            if (xin == 0)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-禁用");
                                            }
                                            else if (xin == 1)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-短信");
                                                var ip = new byte[6];
                                                byteBlock.Read(ip);
                                                var port = new byte[3];
                                                byteBlock.Read(port);
                                                Console.WriteLine($"{Helper.ByteToBCDInt(ip)}:{Helper.ByteToBCDInt(port)}");
                                            }
                                            else if (xin == 2)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-IPV4");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 3)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-北斗");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 4)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-海事卫星");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 5)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-PSTN");
                                                byteBlock.Read(addr);
                                            }
                                            else if (xin == 6)
                                            {
                                                Console.WriteLine($"信道类型：{xin}-超短波");
                                                byteBlock.Read(addr);
                                            }
                                            break;
                                        }
                                    //工作方式
                                    case 0x0C:
                                        {
                                            var workType = byteBlock.ReadByte();
                                            var wt = Helper.ByteToBCDInt(workType);
                                            if (wt == 1)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-自报工作状态");
                                            }
                                            else if (wt == 2)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-自报确认工作状态");
                                            }
                                            else if (wt == 3)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-查询/应答工作状态");
                                            }
                                            else if (wt == 4)
                                            {
                                                Console.WriteLine($"工作方式:{wt}-调试或维修状态");
                                            }
                                            break;
                                        }
                                    //遥测站采集要素设置
                                    case 0x0D:
                                        {
                                            var a1 = byteBlock.ReadByte();
                                            var a1bin = Helper.ByteToBinString(a1);
                                            var a1str = $"降水量：{a1bin[0]}，蒸发量：{a1bin[1]}，风向：{a1bin[2]}，风速：{a1bin[3]}，气温：{a1bin[4]}，湿度：{a1bin[5]}，地温：{a1bin[6]}，气压：{a1bin[7]}";
                                            Console.WriteLine(a1str);

                                            var a2 = byteBlock.ReadByte();
                                            var a2bin = Helper.ByteToBinString(a2);
                                            var a2str = $"水位8：{a2bin[0]}，水位7：{a2bin[1]}，水位6：{a2bin[2]}，水位5：{a2bin[3]}，水位4：{a2bin[4]}，水位3：{a2bin[5]}，水位2：{a2bin[6]}，水位1：{a2bin[7]}";
                                            Console.WriteLine(a2str);

                                            var a3 = byteBlock.ReadByte();
                                            var a3bin = Helper.ByteToBinString(a3);
                                            var a3str = $"地下水埋深：{a3bin[0]}，图片：{a3bin[1]}，波浪：{a3bin[2]}，闸门开度：{a3bin[3]}，水量：{a3bin[4]}，流速：{a3bin[5]}，流量：{a3bin[6]}，水压：{a3bin[7]}";
                                            Console.WriteLine(a3str);

                                            var a4 = byteBlock.ReadByte();
                                            var a4bin = Helper.ByteToBinString(a4);
                                            var a4str = $"水表8：{a4bin[0]}，水表7：{a4bin[1]}，水表6：{a4bin[2]}，水表5：{a4bin[3]}，水表4：{a4bin[4]}，水表3：{a4bin[5]}，水表2：{a4bin[6]}，水表1：{a4bin[7]}";
                                            Console.WriteLine(a4str);

                                            var a5 = byteBlock.ReadByte();
                                            var a5bin = Helper.ByteToBinString(a5);
                                            var a5str = $"100厘米墒情：{a5bin[0]}，80厘米墒情：{a5bin[1]}，60厘米墒情：{a5bin[2]}，50厘米墒情：{a5bin[3]}，40厘米墒情：{a5bin[4]}，30厘米墒情：{a5bin[5]}，20厘米墒情：{a5bin[6]}，10厘米墒情：{a5bin[7]}";
                                            Console.WriteLine(a5str);

                                            var a6 = byteBlock.ReadByte();
                                            var a6bin = Helper.ByteToBinString(a6);
                                            var a6str = $"pH值：{a6bin[0]}，溶解氧：{a6bin[1]}，电导率：{a6bin[2]}，浊度：{a6bin[3]}，氧化还原电位：{a6bin[4]}，高猛酸盐指数：{a6bin[5]}，氨氮：{a6bin[6]}，水温：{a5bin[7]}";
                                            Console.WriteLine(a6str);

                                            var a7 = byteBlock.ReadByte();
                                            var a7bin = Helper.ByteToBinString(a7);
                                            var a7str = $"总有机碳：{a7bin[0]}，总氮：{a7bin[1]}，总磷：{a7bin[2]}，锌：{a7bin[3]}，硒：{a7bin[4]}，砷：{a7bin[5]}，总汞：{a7bin[6]}，镉：{a7bin[7]}";
                                            Console.WriteLine(a7str);

                                            var a8 = byteBlock.ReadByte();
                                            var a8bin = Helper.ByteToBinString(a8);
                                            var a8str = $"{a8bin[0]}，{a8bin[1]}，{a8bin[2]}，{a8bin[3]}，{a8bin[4]}，叶绿素a：{a8bin[5]}，铜：{a8bin[6]}，铅：{a8bin[7]}";
                                            Console.WriteLine(a8str);

                                            break;
                                        }
                                    //中继站(集合转发站)服务地址范围
                                    case 0x0E:
                                        {
                                            var addr = new byte[dlen];
                                            byteBlock.Read(addr);
                                            for (int i = 0; i < addr.Length; i += 6)
                                            {
                                                var start = addr.Skip(i).Take(6).ToArray();
                                                var num = start.Last();
                                                Console.WriteLine($"起始地址：{Helper.ToHEXString(start[0..5])},站数：{Helper.ByteToBCDInt(num)}");
                                            }
                                            break;
                                        }
                                    //遥测站通信设备识别号
                                    case 0x0F:
                                        {
                                            var sim = byteBlock.ReadByte();
                                            //var st = Helper.ByteToBCDInt(sim);
                                            var st = (char)sim;
                                            if (st == '1')
                                            {
                                                Console.WriteLine($"卡类型：{st}-移动通信卡");
                                            }
                                            else if (st == '2')
                                            {
                                                Console.WriteLine($"卡类型：{st}-北斗卫星通信卡");
                                            }
                                            var simcode = new byte[dalen - 1];
                                            byteBlock.Read(simcode);
                                            //Console.WriteLine($"卡识别号：{Helper.ToHEXString(simcode)}");
                                            Console.WriteLine($"卡识别号：{Encoding.ASCII.GetString(simcode)}");

                                            break;
                                        }
                                    default:
                                        break;
                                }

                                //读取下一个标识符
                                elemCode = byteBlock.ReadByte();
                            }
                        }
                        break;
                    case FunctionCode.FC_42:
                    case FunctionCode.FC_43:
                        //运行参数表
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            //读取一个标识符
                            var elemCode = byteBlock.ReadByte();
                            while (elemCode != 0x03 && elemCode != 0x17)
                            {
                                //读取数据定义
                                var dlen = byteBlock.ReadByte();
                                var dalen = dlen >> 3;
                                var dalen1 = dlen & 0x07;
                                //Console.WriteLine($"数据定义:{dlen},字节数:{dalen},小数位数:{dalen1}");

                                var (d, pre) = RunConfigInfo.GetValueType(elemCode);
                                var (name, unit) = RunConfigInfo.Instance.GetInfo((RunConfigCode)elemCode);
                                if (d != 0)
                                {
                                    var d1b = new byte[d];
                                    byteBlock.Read(d1b);
                                    var d1 = Helper.ToHEXString(d1b);
                                    var num1 = Helper.HEXToBCDInt(d1);
                                    decimal value;
                                    if (pre != 0)
                                    {
                                        value = num1 / pre;
                                    }
                                    else
                                    {
                                        value = Convert.ToDecimal(num1);
                                    }
                                    Console.WriteLine($"运行参数标识符:{Helper.ToHEXString(elemCode)} {(ElementCode)elemCode},名称:{name},数据:{value}{unit}");
                                }
                                else
                                {
                                    Console.WriteLine($"运行参数标识符:{Helper.ToHEXString(elemCode)} {(ElementCode)elemCode},名称:{name}");
                                }

                                //读取下一个要素标识符
                                elemCode = byteBlock.ReadByte();
                            }

                            break;
                        }
                    case FunctionCode.FC_45:
                        //软件版本
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var vr = byteBlock.ReadByte();
                            List<byte> bytes = new List<byte>();
                            var b = byteBlock.ReadByte();
                            while (b != 0x03)
                            {
                                bytes.Add(b);
                                b = byteBlock.ReadByte();
                            }
                            Console.WriteLine(Encoding.ASCII.GetString(bytes.ToArray()));
                            //var vrinfo = byteBlock.ReadToArray(vr);
                            //Console.WriteLine(Encoding.ASCII.GetString(vrinfo));
                            //var etx = byteBlock.ReadByte();

                            break;
                        }
                    case FunctionCode.FC_46:
                    case FunctionCode.FC_4B:
                        //遥测站状态和报警信息
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var elemCode = byteBlock.ReadByte();
                            var (name, unit) = ElementInfo.Instance.GetInfo((ElementCode)elemCode);
                            Console.WriteLine(name);
                            if (elemCode == (byte)ElementCode.ZT)
                            {
                                //byteBlock.Seek(1, SeekOrigin.Current);
                                //读取数据定义
                                var dlen = byteBlock.ReadByte();
                                var dalen = dlen >> 3;
                                var dalen1 = dlen & 0x07;

                                var tHEX = new byte[4];
                                byteBlock.Read(tHEX);
                                string binStr = Helper.ByteToBinString(tHEX);
                                //将字符串转成字符数组并反专顺序
                                //因为状态定义是从低位字节开始，故将顺序反转后可从0开始读取
                                //如不反转则从末尾^1开始读取
                                var strArr = binStr.ToCharArray();
                                Array.Reverse(strArr);
                                Console.WriteLine(binStr);

                                if (strArr[0] == '0')
                                {
                                    Console.WriteLine($"交流电充电状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("交流电充电状态：停电");
                                }
                                if (strArr[1] == '0')
                                {
                                    Console.WriteLine("蓄电池电压状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("蓄电池电压状态：电压低");
                                }
                                if (strArr[2] == '0')
                                {
                                    Console.WriteLine("水位超限报警状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("水位超限报警状态：报警");
                                }
                                if (strArr[3] == '0')
                                {
                                    Console.WriteLine("流量超限报警状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("流量超限报警状态：报警");
                                }
                                if (strArr[4] == '0')
                                {
                                    Console.WriteLine("水质超限报警状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("水质超限报警状态：报警");
                                }
                                if (strArr[5] == '0')
                                {
                                    Console.WriteLine("流量仪表状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("流量仪表状态：故障");
                                }
                                if (strArr[6] == '0')
                                {
                                    Console.WriteLine("水位仪表状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("水位仪表状态：故障");
                                }
                                if (strArr[7] == '0')
                                {
                                    Console.WriteLine("终端箱门状态：开启");
                                }
                                else
                                {
                                    Console.WriteLine("终端箱门状态：关闭");
                                }
                                if (strArr[8] == '0')
                                {
                                    Console.WriteLine("存储器状态：正常");
                                }
                                else
                                {
                                    Console.WriteLine("存储器状态：异常");
                                }
                                if (strArr[9] == '0')
                                {
                                    Console.WriteLine("IC卡功能有效：关闭");
                                }
                                else
                                {
                                    Console.WriteLine("IC卡功能有效：IC卡有效");
                                }
                                if (strArr[10] == '0')
                                {
                                    Console.WriteLine("水泵工作状态：水泵工作");
                                }
                                else
                                {
                                    Console.WriteLine("水泵工作状态：水泵停机");
                                }
                                if (strArr[11] == '0')
                                {
                                    Console.WriteLine("剩余水量报警：未超限");
                                }
                                else
                                {
                                    Console.WriteLine("剩余水量报警：水量超限");
                                }

                            }
                            break;
                        }
                    case FunctionCode.FC_47://初始化固态存储数据
                    case FunctionCode.FC_48://恢复出厂设置
                    case FunctionCode.FC_4A://设置遥测站时钟
                    case FunctionCode.FC_51://查询遥测站时钟
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }
                            break;
                        }
                    case FunctionCode.FC_49:
                        //修改密码
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var elemCode = byteBlock.ReadByte();
                            if (elemCode == 0x03)
                            {
                                var pwd = byteBlock.ReadInt16(EndianType.Big);
                                Console.WriteLine($"新密码:{pwd}");
                            }
                            break;
                        }
                    case FunctionCode.FC_4C:
                        //水泵状态
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var l = byteBlock.ReadByte();
                            var bl = new byte[l];
                            byteBlock.Read(bl);
                            var bs = Helper.ByteToBinString(bl);
                            //将二进制字符串转换位字符数组并反转顺序
                            //因为状态定义是从低位字节开始，故将顺序反转后可从0开始读取
                            //如不反转则从末尾^1开始读取
                            var arr = bs.ToCharArray();
                            Array.Reverse(arr);
                            for (int i = 0; i < arr.Length; i++)
                            {
                                string t = arr[i] == '0' ? "关闭" : "开启";
                                Console.Write($"{i + 1}号水泵状态：{bs[i]}-{t}");
                            }
                            break;
                        }
                    case FunctionCode.FC_4D:
                        //阀门状态
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var l1 = byteBlock.ReadByte();
                            var bl1 = new byte[l1];
                            byteBlock.Read(bl1);
                            var bs1 = Helper.ByteToBinString(bl1);
                            //将二进制字符串转换位字符数组并反转顺序
                            //因为状态定义是从低位字节开始，故将顺序反转后可从0开始读取
                            //如不反转则从末尾^1开始读取
                            var arr1 = bs1.ToCharArray();
                            Array.Reverse(arr1);
                            for (int i = 0; i < arr1.Length; i++)
                            {
                                string t = arr1[i] == '0' ? "关闭" : "开启";
                                Console.Write($"{i + 1}号阀门状态：{arr1[i]}-{t}");
                            }
                            break;
                        }
                    case FunctionCode.FC_4E:
                        //闸门状态
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var jl = byteBlock.ReadByte();
                            var j1 = jl / 8;
                            if (jl % 8 != 0)
                            {
                                j1 += 1;
                            }
                            var jm = new byte[j1];
                            byteBlock.Read(jm);
                            var jms = Helper.ByteToBinString(jm);
                            //将二进制字符串转换位字符数组并反转顺序
                            //因为状态定义是从低位字节开始，故将顺序反转后可从0开始读取
                            //如不反转则从末尾^1开始读取
                            var arr = jms.ToCharArray();
                            Array.Reverse(arr);
                            for (int i = 0; i < arr.Length; i++)
                            {
                                string t = arr[i] == '0' ? "关闭" : "开启";
                                Console.Write($"{i + 1}号闸门状态：{arr[i]}-{t}");
                            }

                            for (int i = 0; i < jl; i++)
                            {
                                var kd = new byte[2];
                                byteBlock.Read(kd);
                                var v = Helper.ByteToBCDInt(kd);
                                Console.WriteLine($"{i + 1}号闸门开度：{v}cm");
                            }

                            break;
                        }
                    case FunctionCode.FC_4F:
                        //水量定值
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            var cz = byteBlock.ReadByte();
                            if (cz == 0xFF)
                            {
                                Console.WriteLine("定值控制投入");
                            }
                            else if (cz == 0x00)
                            {
                                Console.WriteLine("定值控制退出");
                            }
                            break;
                        }
                    case FunctionCode.FC_50:
                        //遥测站事件记录
                        {
                            var code = byteBlock.ReadByte();
                            if (code == 0xf1)
                            {
                                byteBlock.Seek(1, SeekOrigin.Current);
                                var f = new byte[5];
                                byteBlock.Read(f);
                                var siteCode1 = Helper.ToHEXString(f);
                                Console.WriteLine($"遥测站地址:{siteCode1}");
                            }
                            else
                            {
                                Console.WriteLine("测站地址引导符错误");
                            }

                            for (int i = 1; i < 33; i++)
                            {
                                switch (i)
                                {
                                    case 1:
                                        var num1 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-历史数据初始化记录：{num1}");
                                        break;
                                    case 2:
                                        var num2 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-参数变更记录：{num2}");
                                        break;
                                    case 3:
                                        var num3 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-状态量变位记录：{num3}");
                                        break;
                                    case 4:
                                        var num4 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-传感器及仪表故障记录：{num4}");
                                        break;
                                    case 5:
                                        var num5 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-密码修改记录：{num5}");
                                        break;
                                    case 6:
                                        var num6 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-终端故障记录：{num6}");
                                        break;
                                    case 7:
                                        var num7 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-交流失电记录：{num7}");
                                        break;
                                    case 8:
                                        var num8 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-蓄电池电压低告警记录：{num8}");
                                        break;
                                    case 9:
                                        var num9 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-终端箱非法打开记录：{num9}");
                                        break;
                                    case 10:
                                        var num10 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-水泵故障记录：{num10}");
                                        break;
                                    case 11:
                                        var num11 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-剩余水量超限告警记录：{num11}");
                                        break;
                                    case 12:
                                        var num12 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-水位超限告警记录：{num12}");
                                        break;
                                    case 13:
                                        var num13 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-水压超限告警记录：{num13}");
                                        break;
                                    case 14:
                                        var num14 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-水质参数超限告警记录：{num14}");
                                        break;
                                    case 15:
                                        var num15 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-数据出错记录：{num15}");
                                        break;
                                    case 16:
                                        var num16 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-发报文记录：{num16}");
                                        break;
                                    case 17:
                                        var num17 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-收报文记录：{num17}");
                                        break;
                                    case 18:
                                        var num18 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-发报文出错记录：{num18}");
                                        break;
                                    default:
                                        var num19 = byteBlock.ReadUInt16(EndianType.Big);
                                        Console.WriteLine($"ERC{i}-：{num19}");
                                        break;
                                }
                            }

                            break;
                        }
                    default:
                        break;

                }

                //如果不是链路维持报则发送响应报文
                if (header.FunctionCode != FunctionCode.FC_2F)
                {
                    var send = BuildResponse.CreatResponse(header, serialnum, EndMarks.ESC);

                    var shex = BitConverter.ToString(send);
                    Console.WriteLine($"\n发送16进制：{shex}");

                    await sessionClient.SendAsync(send);
                }
            }

            e.Handled = true;//表示该数据已经被本插件处理，无需再投递到其他插件。

            await e.InvokeNext();//如果本插件无法处理当前数据，请将数据转至下一个插件。
        }

    }
}
